package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.anno.Log;
import org.example.pojo.Emp;
import org.example.pojo.EmpQueryParm;
import org.example.pojo.PageResult;
import org.example.pojo.Result;
import org.example.service.EmpService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

/**
 * 员工管理Controller
 */
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    private final EmpService empService;

    public EmpController(EmpService empService) {
        this.empService = empService;
    }

    @GetMapping
//    public Result list(@RequestParam(defaultValue = "1") Integer page,
//                       @RequestParam(defaultValue = "10") Integer pageSize,
//                       String name,
//                       Integer gender,
//                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
//                       @DateTimeFormat(pattern = "yyyy-MM-") LocalDate end) {
//        log.info("分页查询:{},{},{},{},{},{}", page, pageSize, name, gender, begin, end);
//        PageResult<Emp> pageResult = empService.findByPage(page, pageSize, name, gender,
//                begin, end);
//        return Result.success(pageResult);
//    }
    //分页查询
    public Result list(EmpQueryParm empQueryParm)  {
        log.info("分页查询:{}",empQueryParm);
        PageResult<Emp> pageResult = empService.findByPage(empQueryParm);
        return Result.success(pageResult);
    }
    //增加员工
    @PostMapping
    @Log
    public Result save(@RequestBody Emp emp) {
        log.info("新增员工:{}", emp);
        empService.save(emp);
        return Result.success();
    }
    //删除员工
    @Log
    @DeleteMapping
//    public Result delete(Integer[] ids) {
    public Result delete(@RequestParam List<Integer> ids) {
        log.info("删除员工:{}", ids);
        empService.delete(ids);
        return Result.success();
    }
    //查询员工
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {//拿取路径上的
        log.info("查询员工:{}", id);
        Emp emp = empService.findById(id);
        return Result.success(emp);
    }
    //修改员工
    @Log
    @PutMapping
    public Result update(@RequestBody Emp emp) {
        log.info("修改员工:{}", emp);
        empService.update(emp);
        return Result.success();
    }
    @GetMapping("/list")
    public Result list() {
        log.info("查询所有员工");
        List<Emp> empList = empService.findAll();
        return Result.success(empList);
    }
}
